<template>
    <el-container>
      <el-header>
        <h3>招生学员信息</h3>
      </el-header>
      <el-main>
        <el-form :inline="true" ref="form" :model="form">
          <el-form-item prop="query_field">
            <el-input type="text" placeholder="输入学员姓名或者手机号" v-model="form.query_field" @keyup.enter.native="search"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click.prevent="search">查询</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click.prevent="reset('form')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="reward"
          border
          :cell-style="changeStyle"
        >
          <el-table-column
            label="总奖励"
            prop="total_money"
          >
          </el-table-column>
          <el-table-column
            label="已奖励"
            prop="has_pay_money"
          >
          </el-table-column>
          <el-table-column
            label="未结算奖励"
            prop="not_pay_money"
          >
          </el-table-column>
          <el-table-column
            label="招生人数"
            prop="enrolment_number"
          >
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <template slot-scope="scope">
              <el-button v-if="scope.row.not_pay_money !== 0" type="primary" icon="el-icon-refresh" @click.prevent="clearMoney">结算</el-button>
              <el-button v-else type="primary" icon="el-icon-refresh" disabled>结算</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-table
        border
        :data="tableData"
        v-loading="loading"
        >
          <el-table-column
            label="头像"
          >
            <template slot-scope="scope">
              <el-avatar :src="scope.row.avatar" :size="50"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column
            label="姓名"
            prop="name"
          ></el-table-column>
          <el-table-column
            label="手机号"
            prop="mobile"
          ></el-table-column>
          <el-table-column
            label="推荐人"
            prop="coach_name"
          ></el-table-column>
          <el-table-column
            label="进度"
            prop="degree"
          ></el-table-column>
          <el-table-column
            label="添加时间"
            prop="start_time"
          ></el-table-column>
        </el-table>
      </el-main>
      <el-footer>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="page"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </el-footer>
    </el-container>
</template>

<script>
  import {enrolmentInfo, enrolmentList, clearCoachReward} from '@/api/coach'
    export default {
        name: "enrollmentList",
        inject: ['reload'],
        data () {
            return {
              form: {
                  query_field: '',
              },
              reward: [],
              tableData: [],
              page: 1,
              total: 0,
              pageSize: 10,
              coach_id: 0,
              loading: true,
            };
        },
        methods: {
          search() {
            var params = {
                coach_id: this.coach_id
            };
            if (this.form.query_field.trim().length != 0) {
                params.query_field = this.form.query_field.trim();
            }
            this.loading = true;
            enrolmentList(params).then(response => {
                this.tableData = response.data.list;
                this.page = Number(response.data.paginate.current);
                this.pageSize = Number(response.data.paginate.pageSize);
                this.total = Number(response.data.paginate.total);
                this.loading = false;
            }).catch(err => {
                this.loading = false;
            })

          },
          reset(ref) {
              this.$refs[ref].resetFields();
          },
          changeStyle(row, column, rowIndex, columnIndex) {
              if (row.column.label == '已奖励') {
                  return 'color: green';
              } else if (row.column.label == '未结算奖励') {
                  return 'color: red';
              }
          },
          changePage (currentValue) {
            var params = {
                page: currentValue,
                pageSize: this.pageSize,
                coach_id: this.coach_id
            };

            if (this.form.query_field.trim().length != 0) {
                params.query_field = this.form.query_field.trim();
            }
            this.loading = true;
            enrolmentList(params).then(response => {
                this.tableData = response.data.list;
                this.page = Number(response.data.paginate.current);
                this.pageSize = Number(response.data.paginate.pageSize);
                this.total = Number(response.data.paginate.total);
                this.loading = false;
            }).catch(err => {
                this.loading = false;
            })
          },
          clearMoney() {
              clearCoachReward({coach_id: this.coach_id}).then(response => {
                this.$message.success(response.msg);
                this.reload();
              }).catch( err => {

              })
          }
        },
        mounted() {
            this.coach_id = this.$route.query.coach_id;
            enrolmentInfo({coach_id: this.coach_id}).then(response => {
                this.reward.push(response.data);
                enrolmentList({coach_id: this.coach_id}).then(response => {
                  this.tableData = response.data.list;
                  this.page = response.data.paginate.current;
                  this.pageSize = response.data.paginate.pageSize;
                  this.total = response.data.paginate.total;
                  this.loading = false;
                })
            }).catch( err => {

            })
        }
    }
</script>

<style scoped>
  .el-container {
    width: 95%;
    max-width: 100%;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }
  .el-header {
    text-align: center;
  }
  .el-header {
    font-size: 20px;
    height: 20px;
    line-height: 20px;
  }
  .el-footer {
    text-align: center;
  }
</style>
